package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author:hx
 * @DateTime:2022/11/8 9:03
 * @Description:
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void saveDepartment(Map<String, Object> paramMap) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);

        //根据医院编号 和 科室编号查询
        Department departmentExist = departmentRepository.
                getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());
        //        //判断
        if(departmentExist!=null) {
            department.setCreateTime(departmentExist.getCreateTime());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            department.setId(departmentExist.getId());
            departmentRepository.save(department);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> findDepartment(Map<String, Object> paramMap) {
        Integer pageNum = JSONObject.parseObject(JSONObject.toJSONString(paramMap.get("page")), Integer.class);
        Integer pageSize = JSONObject.parseObject(JSONObject.toJSONString(paramMap.get("limit")),Integer.class);
        String hoscode = JSONObject.parseObject(JSONObject.toJSONString(paramMap.get("hoscode")),String.class);
        Department department = new Department();
        department.setHoscode(hoscode);
        department.setIsDeleted(0);
        Example<Department> example = Example.of(department);
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        Page<Department> page = departmentRepository.findAll(example,pageable);
        return page;
    }

    @Override
    public void removeDepartment(Map<String, Object> paramMap) {
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        department.setIsDeleted(1);
        departmentRepository.save(department);
    }

    @Override
    public List<DepartmentVo> getDeptByHoscode(String hoscode) {
        List<DepartmentVo> departmentVoList = new ArrayList<>();
        //根据医院hoscode获取科室
        Department department = new Department();
        department.setHoscode(hoscode);
        Example<Department> example = Example.of(department);
        List<Department> all = departmentRepository.findAll(example);
        //将科室分类
        Map<String, List<Department>> collect = all.stream().collect(Collectors.groupingBy(Department::getBigcode));
        //取出大科室和小科室
        Set<Map.Entry<String, List<Department>>> entries = collect.entrySet();
        for (Map.Entry<String, List<Department>> entry : entries) {
            //大科室
            DepartmentVo bigDepartmentVo = new DepartmentVo();
            String key = entry.getKey();
            List<Department> value = entry.getValue();
            bigDepartmentVo.setDepcode(key);
            bigDepartmentVo.setDepname(value.get(0).getBigname());

            //小科室
            List<DepartmentVo> smallDepartmentList = new ArrayList<>();
            for (Department department1 : value) {
                DepartmentVo smallDepartment = new DepartmentVo();
                BeanUtils.copyProperties(department1,smallDepartment);
                smallDepartmentList.add(smallDepartment);
            }
            bigDepartmentVo.setChildren(smallDepartmentList);
            departmentVoList.add(bigDepartmentVo);
        }
        return departmentVoList;
    }
}
